package com.linxianqin.springbootVue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.constant.UserConstants;
import com.linxianqin.springbootVue.domain.convert.SysDictTypeConvert;
import com.linxianqin.springbootVue.domain.convert.SysRoleConvert;
import com.linxianqin.springbootVue.domain.dto.SysDictTypeDTO;
import com.linxianqin.springbootVue.domain.entity.SysDictData;
import com.linxianqin.springbootVue.domain.entity.SysDictType;
import com.linxianqin.springbootVue.domain.entity.SysRole;
import com.linxianqin.springbootVue.domain.query.SysDictTypeQuery;
import com.linxianqin.springbootVue.mapper.SysDictTypeMapper;
import com.linxianqin.springbootVue.service.SysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxianqin.springbootVue.utils.DateUtils;
import com.linxianqin.springbootVue.utils.DictUtils;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author lxq
 * @since 2020-12-14
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Override
    public Page<SysDictTypeDTO> selectSysDictTypePage(Page<SysDictTypeDTO> page, SysDictTypeQuery query) {
        Page<SysDictType> entityPage = SysDictTypeConvert.INSTANCE.dto2entity(page);
        LambdaQueryWrapper<SysDictType> queryWrapper=new QueryWrapper<SysDictType>().lambda();
        if (StringUtils.isNotNull(query.getStatus())){
            queryWrapper.eq(SysDictType::getStatus,query.getStatus());
        }
        if (StringUtils.isNotNull(query.getDictType())){
            queryWrapper.like(SysDictType::getDictType,query.getDictType());
        }
        if (StringUtils.isNotNull(query.getDictName())){
            queryWrapper.like(SysDictType::getDictName,query.getDictName());
        }
        if (StringUtils.isNotNull(query.getParams())){
            Map<String, Object> params = query.getParams();
            Object beginTime = params.get("beginTime");
            if (beginTime!=null){
                queryWrapper.ge(SysDictType::getCreateTime,beginTime);
            }
            Object endTime = params.get("endTime");
            if (beginTime!=null){
                endTime = DateUtils.addOneDay(DateUtils.dateTime("yyyy-MM-dd", endTime.toString()));
                queryWrapper.le(SysDictType::getCreateTime,endTime);
            }
        }
        Page<SysDictType> selectPage = baseMapper.selectPage(entityPage, queryWrapper);
        return SysDictTypeConvert.INSTANCE.entity2dto(selectPage);
    }

    @Override
    public String checkDictTypeUnique(SysDictTypeDTO dictTypeDTO) {
        Long dictId = StringUtils.isNull(dictTypeDTO.getDictId()) ? -1L : dictTypeDTO.getDictId();
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getDictType,dictTypeDTO.getDictType());
        SysDictType sysDictType = baseMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(sysDictType) && sysDictType.getDictId().longValue() != dictId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }
}
